package com.dd;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.ArrayList;

import com.chantel.tool.db.DBUtil;
import com.dd.exception.UnauthorizedException;
import com.dd.exception.UserAlreadyExistsException;
import com.dd.exception.UserNotFoundException;

public class UserManager {
	

	public UserManager() {
	  }

	  public User createUser(String yongHuMing, String miMa, String xingMing, int dengJi)
	      throws UserAlreadyExistsException {
	    User newUser = null;
	    try {
	        User existingUser = getUser(yongHuMing);

	        //The user already exists since now exception, so:
	        throw new UserAlreadyExistsException();
	    }
	    catch (UserNotFoundException unfe) {
	        //The user doesn't already exist so we can create a new user
	        newUser = new User(yongHuMing, miMa, xingMing, dengJi);
	    }
	    newUser.insertIntoDB();
	    return newUser;

	  }
	  //
	  //创建审核人员
	  //一个审核人员属于一个部门,但是可以管理多个项目
	  //
	  public User createShenHeRenYuan(String username,String realName,String miMa,int buMenid,Vector XiangmuIDs)
	  throws UserAlreadyExistsException
	  {
		try
		{
			User newShenHeYuan = createUser(username,miMa,realName,SysGlobals.C_ShenHeYuan);
			int userID = newShenHeYuan.getId();
			System.out.println(userID);
			//添加User和部门的对应关系
			boolean bSucceed = UserToBuMen.intsertNewRelation(userID, buMenid);
			System.out.println(bSucceed);
			
			//添加User和项目的对应关系
			int nCount = XiangmuIDs.size();
			for(int index = 0;index < nCount;index++)
			{
				int nId = ((Integer)XiangmuIDs.get(index)).intValue();
				System.out.println(nId);
				UserToXiangMu.InSertToDBForShenHe(userID, nId);
			}
			
			return newShenHeYuan;
		}
		catch(UserAlreadyExistsException em)
		{
			throw em;
		}
	  }
	  //
	  //创建采购人员
	  //一个采购员只属于一个项目
	  //
	  public User createCaiGouYuan(String username,String realName,String miMa,int XiangMuId)
	  throws UserAlreadyExistsException
	  {
		  try
		  {
		     User newCaiGouYuan = createUser(username,miMa,realName,SysGlobals.C_CaiGouYuan);
		     
		     //添加User和项目的对应关系
		     UserToXiangMu userToXiangMu = new UserToXiangMu(newCaiGouYuan.getId(),XiangMuId);
		     userToXiangMu.InSertToDB();
		     return newCaiGouYuan;
		  } catch(UserAlreadyExistsException em)
		  {
			  throw em;
		  }
	
	  }
	  //
	  //删除用户时要同时删掉该用户和项目 或 部门 的关系.
	  //
	  public void deleteUser(User user) throws UnauthorizedException {
		 
		  boolean blBuMen = true;
		if(user.getDengJi() ==SysGlobals.C_ShenHeYuan)
		{
			blBuMen = UserToBuMen.DeleteUserToBuMen_GuanXi(user.getId());
		}
		boolean blXiangMu = UserToXiangMu.DeleteUserToXiangMu_GuanXi(user.getId());
	    boolean bl = user.deleteUser();
	    if(bl == false || blXiangMu == false || blBuMen == false)
	    {
	    	throw new UnauthorizedException();
	    }
	  }

	  public User getUser(int userID) throws UserNotFoundException {
		  
	    User user = new User(userID);
	    return user;
	  }

	  public User getUser(String userName) throws UserNotFoundException {
	    User user = new User(userName);
	    return user;
	  }

	  public int getUserCount() {
	    //TODO 取得USER总数
		  return -1;
	  }

	  public Iterator users() {
	    //TODO 取得所有USER
		  return null;
	  }


	  public Iterator users(int startIndex, int endIndex) {
	    /**@todo Implement this cn.edu.zufe.lib.UserManager method*/
	    throw new java.lang.UnsupportedOperationException("Method Users() not yet implemented.");
	  }
	  //返回所有审核人员信息
	  public static ArrayList GetAllShenHeusers()
	  {
		  return GetAllUserInfoByDengji(SysGlobals.C_ShenHeYuan);
	  }
	  //返回所有采购员信息
	  public static ArrayList GetAllCaiGouUsers()
	  {
		  return GetAllUserInfoByDengji(SysGlobals.C_CaiGouYuan);
	  }
	  private static ArrayList GetAllUserInfoByDengji(int nDengji)
	  {
		  DBUtil du = DBUtil.getInstance();
			
			ArrayList aL = new ArrayList();
			String sql = "select* from YuanGongInfoTable where DengJi = " + ((Integer)(new Integer(nDengji))).toString();;
			List l = du.getRecords(sql);
			
			Iterator it =  l.iterator();
			while(it.hasNext())
			{
				Map m = (Map)it.next();
				
				int id = ((Integer) m.get("YuanGongId")).intValue();
				String YongHuMing= (String)m.get("YongHuMing");
				String MiMa = (String)m.get("MiMa");
				String XingMing = (String)m.get("XingMing");
				int nDenJI = ((Integer)m.get("DengJi")).intValue();
				
				System.out.print(id);
				System.out.print(YongHuMing);
				System.out.print(MiMa);
				System.out.print(XingMing);
				System.out.print(nDenJI);
				System.out.println();
				
				User us = new User(id,YongHuMing, MiMa, XingMing,nDenJI);
				
				aL.add(us);	
			}
			
			return aL;
	  }
	  //
	  //通过审核人员的username 得到该审核员的权限
	  //
	  public static int GetShenHeYuanQuanXianByUserID(String userName)
	  throws UserNotFoundException
	  {
		  int nQuanXian = SysGlobals.C_ShenHeYuan;
		  
		  try
		  {
			  User us = new User(userName);
			  
			  BuMenInfo buMenInfo = BuMenManager.GetUserBuMenInfoByUserID(us.getId());
			  if (buMenInfo==null)
				  return -1;
			  nQuanXian = buMenInfo.getQuanXian();
		  }
		  catch(UserNotFoundException es)
		  {
			  throw es;
		  }
		  
		  return nQuanXian;
	  }
	  
	  private static int getCaiGouBiaoDanCount(int userId,String strZhuangTai)
	  {
			DBUtil du = DBUtil.getInstance();

			ArrayList aL = new ArrayList();
			String sql = "select* from CaiGouBiaoTable where YuanGongId = " 
						+ ((Integer) (new Integer(userId))).toString()
						+ " and BiaoZhuangTai = " + strZhuangTai;
			List l = du.getRecords(sql);
			int nCount = 0;
			nCount = l.size();

		  return nCount; 
	  }
	  ///
	  //返回等待审核的表单数目
	  //
	  public static int getDengDaiShenHeCaiGouBiaoByUserId(int id)
	  {

		  int nCount = 0;
		  nCount = UserManager.getCaiGouBiaoDanCount(id, "0");

		  return nCount;
	  }
	  
	  ///
	  //返回审核通过的表单数目
	  //
	  public static int getShenHeTongGuoCaiGouBiaoByUserId(int id)
	  {

		  int nCount = 0;
		  nCount = UserManager.getCaiGouBiaoDanCount(id, "1");

		  return nCount;
	  }
	  
	  ///
	  //返回审核拒绝的表单数目
	  //
	  public static int getShenHeJuJueCaiGouBiaoByUserId(int id)
	  {

		  int nCount = 0;
		  nCount = UserManager.getCaiGouBiaoDanCount(id, "2");

		  return nCount;
	  }
	  
	  ///
	  //返回所有的表单数目
	  //
	  public static int getAllCaiGouBiaoByUserId(int id)
	  {

			DBUtil du = DBUtil.getInstance();

			ArrayList aL = new ArrayList();
			String sql = "select* from CaiGouBiaoTable where YuanGongId = " 
						+ ((Integer) (new Integer(id))).toString();
			List l = du.getRecords(sql);
			int nCount = 0;
			nCount = l.size();

		  return nCount; 
	  }
	  
	  //测试
		public static void main(String[] args){
			
			//ArrayList caigou = UserManager.GetAllCaiGouUsers();
			//ArrayList shenhe = UserManager.GetAllShenHeusers();
			
			int ncount = UserManager.getDengDaiShenHeCaiGouBiaoByUserId(2);
		}
}
